package com.cl.base.util;

import com.cl.base.constant.ResultCode;
import com.cl.base.constant.StatusCode;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serial;
import java.io.Serializable;

/**
 * 响应信息主体
 */
@Getter
@Setter
@Accessors(chain = true)
public class ResultVo implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;
    // 状态码
    private int code;

    // 状态信息
    private String msg;

    // 返回对象
    private Object data;

    public ResultVo(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    // 默认返回成功状态码，数据对象
    public static ResultVo ok(Object data) {
        return resultVo(ResultCode.SUCCESS, data);
    }

    public static ResultVo fail(Object data) {
        return resultVo(ResultCode.FAILED, data);
    }

    public static ResultVo fail(String msg) {
        return resultVo(ResultCode.FAILED.getCode(), msg);
    }

    public static ResultVo ok() {
        return resultVo(ResultCode.SUCCESS);
    }

    public static ResultVo fail() {
        return resultVo(ResultCode.FAILED);
    }

    // 返回指定状态码，数据对象
    public static ResultVo resultVo(StatusCode statusCode, Object data) {
        return resultVo(statusCode.getCode(), statusCode.getMsg(), data);
    }

    public static ResultVo resultVo(int code, String msg, Object data) {
        return new ResultVo(code, msg, data);
    }

    public static ResultVo resultVo(int code, String msg) {
        return new ResultVo(code, msg, null);
    }

    // 只返回状态码
    public static ResultVo resultVo(StatusCode statusCode) {
        return resultVo(statusCode, null);
    }

}
